/**
 * 功能描述  保存
 */

define(function (require, exports, module) {
	var aw = require("ajaxwrapper");
	var store = require("store");
	var Dialog = require("dialog");
	var activeUser = store.get("user");
	
    module.exports = {
            saveAction : function(widget){
            	var editGridData = widget.get("editgrid").getData();
            	
            	var formData = widget.get("form").getData();
            	var subnav = widget.get("subnav");
            	
            	var groupUnit = formData.groupUnit;
            	var groupType = formData.groupType;
            	var pkCIImplement = formData.pkCIImplement;
            	var search = subnav.getValue("search");
            	var destination = subnav.getValue("destination");
            	var servicePoint = subnav.getValue("servicePoint");
            	var rangeDate = subnav.getValue("time");
            	var subnav = widget.get("subnav");
            	
            	var saveData = {
            			"changeRoomViews" : editGridData,
            			"servicePoint" : subnav.getValue("servicePoint"),
            			"operator" : activeUser.pkUser
            	};
            	aw.saveOrUpdate("api/changeroom/saveall", aw.customParam(saveData), function(datas){
            		widget.get("editgrid").refresh({
						 "groupType" : groupType,
						 "groupUnit" : groupUnit,
						 "pkCIImplement" : pkCIImplement,
						 "contactName" : search,
						 "destination" : destination,
						 "servicePoint" : servicePoint,
              	         "startDate" : rangeDate.start,
              	         "endDate" : rangeDate.end,
              	          fetchProperties:"*,croomType.name,croom.code"
           			 },function(data){
           				widget.hide([".J-form",".J-editgrid",".J-detailgrid"]).show([".J-grid"]);
						widget.get("subnav").show(["search", "servicePoint", "destination", "time"]).hide(["save", "return"]);
						widget.get("subnav").setValue("search", "");
						widget.get("grid").refresh();
           			 });
            	});
            },
            saveConsum : function(widget){
            	if(widget.get("waterelectricform").getValue("waterElectric") && 
            			!widget.get("waterelectricform").valid()){
            		return false;
            	}
            	var roomConsumptionData = widget.get("roomConsumpEdit").getData();
        		var damagesConsumptionData = widget.get("damagesConsumpEdit").getData();
        		var consumptionData = [];
        		for(var i=0; i < roomConsumptionData.length; i++){
        			if(typeof(roomConsumptionData[i].consumptionType) != "string"){
        				roomConsumptionData[i].consumptionType = roomConsumptionData[i].consumptionType.key;
        			};
        			if(typeof(roomConsumptionData[i].goods) != "string"){
        				roomConsumptionData[i].goods = roomConsumptionData[i].goods.pkGoods;
        			};
        			consumptionData.push(roomConsumptionData[i]);
        		}
        		for(var i=0; i < damagesConsumptionData.length; i++){
        			if(typeof(damagesConsumptionData[i].consumptionType) != "string"){
        				damagesConsumptionData[i].consumptionType = damagesConsumptionData[i].consumptionType.key;
        			};
        			if(typeof(damagesConsumptionData[i].goods) != "string"){
        				damagesConsumptionData[i].goods = damagesConsumptionData[i].goods.pkGoods;
        			};
        			consumptionData.push(damagesConsumptionData[i]);
        		}
        		var weFormData = widget.get("waterelectricform").getData();
        		var changeRoomViews = widget.get("editgrid").getData();
        		for(var i=0; changeRoomViews.length; i++){
        			if(changeRoomViews[i].pkPersonalInfo == widget.get("pkPersonalInfo")){
        				changeRoomViews[i].roomPerConsumptions = consumptionData;
        				changeRoomViews[i].checkInElectric = weFormData.checkInElectric;
        				changeRoomViews[i].checkOutElectric = weFormData.checkOutElectric;
        				changeRoomViews[i].checkInColdWater = weFormData.checkInColdWater;
        				changeRoomViews[i].checkOutColdWater = weFormData.checkOutColdWater;
        				changeRoomViews[i].checkInHotWater = weFormData.checkInHotWater;
        				changeRoomViews[i].checkOutHotWater = weFormData.checkOutHotWater;
        				break;
        			}
        		}
        		widget.get("editgrid").setData(changeRoomViews);
        		widget.show([".J-editgrid",".J-form"]).hide([".J-middleform",".J-consumpeditgrid",".J-consumpdetailgrid"]);
				widget.get("subnav").show(["return","save"]);
				widget.get("subnav").hide(["consumsave","consumreturn"]);
            }
        };
});
